import session from '../../api/session'
import storage from '../../api/storage'


export default {
  namespaced: true,
  state: {
    /**
     * 登录缓存信息
     */
    loginSession: {
      token: '',
      online: false,
      time: new Date(),
      user: '',
      id: 0
    },
    fail_num: 0
  },
  mutations: {
    /**
     * 退出方法
     * @param state 注入的state
     */
    removeSessionMutation (state) {
      state.loginSession = {
        token: '',
        online: false,
        time: new Date(),
        user: '',
        id: 0
      };
      session.remove('loginSession');
      storage.remove('loginSession');
    },
    /**
     * 记住登录
     * @param state 注入的state
     */
    rememberMutation (state) {
      storage.set('loginSession', state.loginSession)
    },
    addFailNumMutation (state) {
      state.fail_num ++;
    },
    clearFailNumMutation (state) {
      state.fail_num = 0;
    },
    /**
     * 更新缓存
     * @param state 注入的state
     * @param data http post返回来的数据（AuthResult）
     */
    updateSessionMutation (state, data) {
      state.loginSession = {
        token: data.token,
        online: data.online,
        time: new Date(),
        user: data.name,
        id: data.id
      };
      session.set('loginSession', state.loginSession)
    }
  },
  actions: {
    /**
     * 通过http获取Token
     * @param context 上下文
     * @param authForm 鉴权表单
     */
    getToken (context, authForm) {

      // if (authForm.phoneNum === '15958041842' && authForm.password === '123456'){
      //   let data = {
      //     token : "this is fake token",
      //     deadLine : new Date()
      //   };
      //   context.commit('setLoginSuccessMsg', data);
      //   if (authForm.remember.length > 0){
      //     context.commit('remember');
      //   }
      // }else {
      //   context.commit('setLoginFailMsg');
      // }


      // loginApi.auth(authForm, {
      //   loginSuccess (res) {
      //     context.commit('setLoginSuccessMsg', res.data)
      //     context.commit('notice/setContent', '登录成功', { root: true })
      //   },
      //   loginFail () {
      //     context.commit('setLoginFailMsg', '用户名或者密码错误')
      //   },
      //   handlerRemember () {
      //     context.commit('remember')
      //   },
      //   loginHandler404 () {
      //     context.commit('setLoginFailMsg', '用户不存在')
      //   },
      //   loginHandler403 () {
      //     context.commit('setLoginFailMsg', '验证码输入错误')
      //   },
      //   loginHandlerOtherError () {
      //     context.commit('setLoginFailMsg', '服务器异常')
      //   }
      // })
    },
    checkToken ({commit, state}) {
      // if (state.loginSession.online) {
      //   loginApi.token(state.loginSession, {
      //     loginFail () {
      //       commit('notice/setContent', '登录失效', { root: true })
      //       commit('setLoginFailMsg', '登录失效')
      //     },
      //     loginHandlerOtherError () {
      //       commit('notice/setContent', '服务器连接失败', { root: true })
      //     }
      //   })
      // }
    },
    refreshToken ({commit, state}) {
      // if (state.loginSession.online) {
      //   loginApi.refresh(state.loginSession, {
      //     loginSuccess (res) {
      //       commit('updateLoginSession', res.data)
      //     },
      //     loginFail () {
      //       commit('setLoginFailMsg', '重新申请令牌失败，请重新登录')
      //     },
      //     loginHandlerOtherError () {
      //       commit('setLoginFailMsg', '重新申请令牌出现网络错误，请重新登录')
      //       this.$Message.info('重新申请令牌出现网络错误，请重新登录')
      //     }
      //   })
      // }
    }
  },
  getters: {
    /**
     * 获取登录信息缓存
     * @param state 自动注入的state
     * @returns {state.loginSession|{token, online, time}|*} 登录信息缓存
     */
    getLoginSessionCache: state => {
      let s = session.get('loginSession');
      let s1 = storage.get('loginSession');
      if (s) {
        state.loginSession = s;
        if (s1) {
          storage.set('loginSession', state.loginSession);
        }
      }
      if (s1) {
        state.loginSession = s1;
      }
      return state.loginSession;
    },
    getLoginSessionState: state => {
      return state.loginSession;
    },
    getFailNumState: state => {
      return state.fail_num;
    },
    getUserName: state => {
      return state.loginSession.user;
    },
    getUserId: state => {
      return state.loginSession.id;
    }
  }
}
